home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _gs / !GS / c / IDEBUG < prev    next >
Text File  |  1991-10-26  |  5KB  |  168 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* idebug.c */
  21. /* Debugging support for Ghostscript interpreter */
  22. #include "string_.h"
  23. #include "ghost.h"
  24. #include "iutil.h"
  25. #include "dict.h"
  26. #include "name.h"
  27. #include "ostack.h"            /* for opdef.h */
  28. #include "opdef.h"
  29. #include "packed.h"
  30. #include "store.h"        /* for make_oper for opdef.h */
  31.  
  32. /* Forward references */
  33. void debug_print_string(P2(byte *, ushort));
  34. #define debug_print_name(pnref)\
  35.   debug_print_string((pnref)->value.pname->string_bytes,\
  36.              (pnref)->value.pname->string_size)
  37.  
  38. /* Print a ref */
  39. void
  40. debug_print_ref(ref *pref)
  41. {    unsigned size = r_size(pref);
  42.     ushort elt;
  43.     ref nref;
  44.     dprintf1("(%x)", r_type_attrs(pref));
  45.     elt = *(ushort *)pref;
  46.     if ( elt <= packed_max_full_ref )
  47.       switch ( r_type(pref) )
  48.        {
  49.     case t_array:
  50.       dprintf2("array(%u)0x%lx", size, (ulong)pref->value.refs); break;
  51.     case t_boolean:
  52.       dprintf1("boolean %x", pref->value.index); break;
  53.     case t_color:
  54.       dprintf1("color 0x%lx", (ulong)pref->value.pcolor); break;
  55.     case t_device:
  56.       dprintf1("device 0x%lx", (ulong)pref->value.pdevice); break;
  57.     case t_dictionary:
  58.       dprintf3("dict(%u/%u)0x%lx",
  59.            dict_length(pref), dict_maxlength(pref),
  60.            (ulong)pref->value.pdict);
  61.       break;
  62.     case t_file: dprintf1("file 0x%lx", (ulong)pref->value.pfile); break;
  63.     case t_integer: dprintf1("int %ld", pref->value.intval); break;
  64.     case t_mark: dprintf("mark"); break;
  65.     case t_mixedarray:
  66.       dprintf2("mixed packedarray(%u)0x%lx", size,
  67.            (ulong)pref->value.packed); break;
  68.     case t_name:
  69.       dprintf2("name(0x%lx#%x)", (ulong)pref->value.pname,
  70.            pref->value.pname->index);
  71.       debug_print_name(pref);
  72.       break;
  73.     case t_null: dprintf("null"); break;
  74.     case t_oparray:
  75.       dprintf1("op_array(0x%x)", size);
  76.       name_index_ref(op_array_nx_table[size - op_def_count], &nref);
  77.       debug_print_name(&nref);
  78.       break;
  79.     case t_operator:
  80.       dprintf1("op(0x%x", size);
  81.       if ( size )
  82.         dprintf1(":%s", (char *)(op_def_table[size]->oname + 1));
  83.       dprintf1(")0x%lx", (ulong)pref->value.opproc);
  84.       break;
  85.     case t_real: dprintf1("real %f", pref->value.realval); break;
  86.     case t_shortarray:
  87.       dprintf2("short packedarray(%u)0x%lx", size,
  88.            (ulong)pref->value.packed); break;
  89.     case t_string:
  90.       dprintf2("string(%u)0x%lx", size, (ulong)pref->value.bytes); break;
  91.     default: dprintf1("type 0x%x", r_type(pref));
  92.        }
  93.     else
  94.       switch ( elt >> packed_type_shift )
  95.        {
  96.     case pt_executable_operator:
  97.       dprintf("<op_name>");
  98.       elt &= packed_int_mask;
  99.       op_index_ref(elt, &nref);
  100.       debug_print_ref(&nref);
  101.       break;
  102.     case pt_integer:
  103.       dprintf1("<int> %d", (elt & packed_int_mask) + packed_min_intval);
  104.       break;
  105.     case pt_literal_name: case pt_literal_name+1:
  106.       dprintf("<lit_name>"); elt &= packed_max_name_index; goto ptn;
  107.     case pt_executable_name: case pt_executable_name+1:
  108.       dprintf("<exec_name>"); elt &= packed_max_name_index;
  109. ptn:      name_index_ref(elt, &nref);
  110.       dprintf2("(0x%lx#%x)", (ulong)nref.value.pname, elt);
  111.       debug_print_name(&nref);
  112.       break;
  113.        }
  114. }
  115.  
  116. /* Print a string */
  117. void
  118. debug_print_string(byte *chrs, ushort len)
  119. {    ushort i;
  120.     for ( i = 0; i < len; i++ )
  121.         dputc(chrs[i]);
  122. }
  123.  
  124. /* Dump a region of memory containing refs */
  125. void
  126. debug_dump_refs(ref *from, ref *to, char *msg)
  127. {    ref *p = from;
  128.     if ( from < to ) dprintf2("%s at 0x%lx:\n", msg, (ulong)from);
  129.     while ( p < to )
  130.        {    unsigned type = r_type(p), attrs = r_type_attrs(p);
  131.         unsigned btype = r_btype(p);
  132.         static char *ts[] = { type_print_strings };
  133.         static char *as = attr_print_string;
  134.         char *ap = as;
  135. #define buf_size 30
  136.         char buf[buf_size + 1];
  137.         uint plen;
  138.         dprintf2("..%04x: 0x%02x ", (uint)p & 0xffff, type);
  139.         if ( btype >= t_next_index )
  140.             dprintf1("0x%02x?? ", btype);
  141.         else
  142.             dprintf1("%s ", ts[btype]);
  143.         for ( ; *ap; ap++, attrs >>= 1 )
  144.           if ( *ap != '.' )
  145.             dputc(((attrs & 1) ? *ap : '-'));
  146.         dprintf2(" 0x%04x 0x%08lx", r_size(p), *(ulong *)&p->value);
  147.         if ( obj_cvs(p, (byte *)buf, buf_size, &plen) >= 0 &&
  148.              ((buf[plen] = 0), strcmp(buf, "--nostringval--"))
  149.            )
  150.             dprintf1(" = %s", buf);
  151.         dputc('\n');
  152.         p++;
  153.        }
  154. }
  155.  
  156. /* Dump a region of memory */
  157. void
  158. debug_dump_bytes(byte *from, byte *to, char *msg)
  159. {    byte *p = from;
  160.     if ( from < to ) dprintf1("%s:\n", msg);
  161.     while ( p != to )
  162.        {    byte *q = min(p + 16, to);
  163.         dprintf1("%lx:", (ulong)p);
  164.         while ( p != q ) dprintf1(" %02x", *p++);
  165.         dputc('\n');
  166.        }
  167. }
  168.